package mi.payment.utils.transaction;

import android.content.Context;
import android.text.TextUtils;

import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.CARDTYPE;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 预授权完成交易
 *
 * @author tomcat on 2017/2/8.
 */
public class PreAuthorizationCompleteTask extends TransactionTask<TradeData, Void, TradeData> {

    protected PreAuthorizationCompleteTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        setCUPDebit(true);
        setDBStorage(true);
        setUpdateSettlementTable(true);
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_PRE_GRANT_COMPLETE);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("000000");
        dbBean.setTransType("20");
        dbBean.setNetManageCode("000");
        dbBean.TerminalDate = System.currentTimeMillis() / 1000;
        dbBean.isNeedReversal = true;
        dbBean.tradeAnswerCode = "96";

        // 更新预组装的Map
        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写60域
        MsgField f060 = map.get("F060");
        StringBuilder value60 = new StringBuilder();
        value60.append(dbBean.getTransType());
        value60.append(dbBean.getBathNo());
        value60.append(dbBean.getNetManageCode());
        String value60_4 = "6";
        if (dbBean.getCardType() == CARDTYPE.IC.getValue()) {
            value60_4 = "5";
        }
        value60.append(value60_4);
        value60.append("0");
        f060.setDataStr(value60.toString());
        f060.setDataLength(value60.length());

        // 61域
        MsgField fieldF061 = map.get("F061");
        String value61 = "";
        if (TextUtils.isEmpty(dbBean.getOriginalBathNo())) {
            value61 += "000000";
        } else {
            value61 += dbBean.getOriginalBathNo();
        }
        if (TextUtils.isEmpty(dbBean.getOriginalPOSNum())) {
            value61 += "000000";
        } else {
            value61 += dbBean.getOriginalPOSNum();
        }
        if (TextUtils.isEmpty(dbBean.getOriginalTransDate())) {
            value61 += "0000";
        } else {
            value61 += dbBean.getOriginalTransDate();
        }
        fieldF061.setDataStr(value61);
        fieldF061.setDataLength(value61.length());

        // 添加自定义的商户号
        map = ParseUtils.addCustomTransID(map);

        // 组包,得到组包后的报文
        return EncodePackage.assembly(map, "0200", TerminalSecure.PROCESSREQUEST_NONE);
    }


}